package com.example.zhugenew;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.zhugenew.fragment.HomeFragment;
import com.example.zhugenew.fragment.InformationFragment;
import com.example.zhugenew.fragment.MeFragment;
import com.example.zhugenew.fragment.MessageFragment;
import com.google.android.material.tabs.TabLayout;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

public class HomeActivity extends AppCompatActivity {

    private FrameLayout mFl;
    private TabLayout mTablayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
    }
    @Override
    public void onResume(){
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause(){
        super.onPause();
        MobclickAgent.onPause(this);
    }
    private void initView() {
        mFl = (FrameLayout) findViewById(R.id.fl);
        mTablayout = (TabLayout) findViewById(R.id.tablayout);
        final ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new MessageFragment());
        fragments.add(new InformationFragment());
        fragments.add(new MeFragment());
        getSupportFragmentManager().beginTransaction().replace(R.id.fl,fragments.get(0)).commit();
        mTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                getSupportFragmentManager().beginTransaction().replace(R.id.fl,fragments.get(position)).commit();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mTablayout.setSelectedTabIndicatorColor(Color.TRANSPARENT);
        mTablayout.setFocusableInTouchMode(false);
        mTablayout.addTab(mTablayout.newTab().setText("首页").setIcon(R.drawable.home_selector));
        mTablayout.addTab(mTablayout.newTab().setText("消息").setIcon(R.drawable.home_selector));
        mTablayout.addTab(mTablayout.newTab().setText("资讯").setIcon(R.drawable.home_selector));
        mTablayout.addTab(mTablayout.newTab().setText("我").setIcon(R.drawable.home_selector));
    }
}